package com.abu.messageboard.service.impl;


import com.abu.messageboard.config.mvc.LoginInterceptor;
import com.abu.messageboard.mapper.MessageMapper;
import com.abu.messageboard.pojo.Message;
import com.abu.messageboard.service.MessageService;
import com.abu.messageboard.utils.PageUtils;
import com.abu.messageboard.vo.MessageVO;
import com.abu.messageboard.vo.QueryMessageBoardVo;
import com.abu.messageboard.vo.UserInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 留言表 服务实现类
 * </p>
 *
 * @author 阿布
 * @since 2021-02-25
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Override
    public HashMap<String, Object> queryByPageAndCondition(QueryMessageBoardVo condition) {
//        获取参数
        Integer order = condition.getOrder();
        Long mgId = condition.getMgId();
        Long page = condition.getPage();
        List<MessageVO> messageVOList;
        HashMap<String, Object> mapResult = new HashMap<>(2);
        Integer count;
        UserInfo userInfo = LoginInterceptor.getUserInfo();
        Long userId = userInfo.getUserId();
//        按审批人查找
        if (mgId != null) {
            messageVOList = this.messageMapper.selectPageByMgId(mgId, page,userId);
            count = this.messageMapper.selectPageCountMgId(mgId,userId);
            mapResult.put("messageVOList", messageVOList);
            mapResult.put("count", count);
            return mapResult;
        }
//        按顺序查找
        if (order != null) {
            if (order == 1) {
                messageVOList = this.messageMapper.selectPageAndOderASCByMgId(mgId, page,userId);
            } else {
                messageVOList = this.messageMapper.selectPageAndOrderDescByMgId(mgId, page,userId);
            }
            count = this.messageMapper.selectPageAndOderByMgIdCount(mgId,userId);
            mapResult.put("messageVOList", messageVOList);
            mapResult.put("count", count);
            return mapResult;
        }
//       初始化查找
        messageVOList = this.messageMapper.selectPage(page,userId);
        count = this.messageMapper.selectPageCount(page,userId);
        mapResult.put("messageVOList", messageVOList);
        mapResult.put("count", count);
        return mapResult;
    }
}
